package com.hewanglan.subject.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.hewanglan.subject.common.enums.CategoryTypeEnum;
import com.hewanglan.subject.common.enums.IsDeletedFlagEnum;
import com.hewanglan.subject.domain.bo.SubjectLabelBO;
import com.hewanglan.subject.domain.convert.SubjectLabelConverter;
import com.hewanglan.subject.domain.service.SubjectLabelDomainService;
import com.hewanglan.subject.infra.basic.entity.SubjectCategory;
import com.hewanglan.subject.infra.basic.entity.SubjectLabel;
import com.hewanglan.subject.infra.basic.service.SubjectCategoryService;
import com.hewanglan.subject.infra.basic.service.SubjectLabelService;
import com.hewanglan.subject.infra.basic.service.SubjectMappingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
@Slf4j
public class SubjectLabelDomainServiceImpl implements SubjectLabelDomainService {
    @Resource
    private SubjectLabelService subjectLabelService;
    @Resource
    private SubjectMappingService subjectMappingService;

    @Resource
    private SubjectCategoryService subjectCategoryService;

    @Override
    public Boolean add(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.add.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBOToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
        int count = subjectLabelService.insert(subjectLabel);
        return count > 0;
    }

    @Override
    public Boolean update(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.update.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBOToLabel(subjectLabelBO);
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    @Override
    public Boolean delete(SubjectLabelBO subjectLabelBO) {
        if (log.isInfoEnabled()) {
            log.info("SubjectLabelDomainServiceImpl.delete.bo:{}", JSON.toJSONString(subjectLabelBO));
        }
        SubjectLabel subjectLabel = SubjectLabelConverter.INSTANCE.convertBOToLabel(subjectLabelBO);
        subjectLabel.setIsDeleted(IsDeletedFlagEnum.DELETED.getCode());
        int count = subjectLabelService.update(subjectLabel);
        return count > 0;
    }

    @Override
    public List<SubjectLabelBO> queryLabelByCategoryId(SubjectLabelBO subjectLabelBO) {
        //如果当前分类是1级分类，则查询所有标签
        SubjectCategory subjectCategory=subjectCategoryService.queryById(subjectLabelBO.getCategoryId());
        if(CategoryTypeEnum.PRIMARY.getCode()==subjectCategory.getCategoryType())
        {
            SubjectLabel subjectLabel=new SubjectLabel();
            subjectLabel.setCategoryId(subjectLabelBO.getCategoryId());
            List<SubjectLabel> labelList=subjectLabelService.queryByCondition(subjectLabel);
            List<SubjectLabelBO> labelResultList = SubjectLabelConverter.INSTANCE.convertLabelToBOList(labelList);
            return labelResultList;
        }
        /**
         *根据categoryId在subjectLabel表中查询标签
         */
        Long categoryId1 = subjectLabelBO.getCategoryId();
        SubjectLabelBO subjectLabelBO1=new SubjectLabelBO();
        subjectLabelBO1.setCategoryId(categoryId1);
        SubjectLabel subjectLabel=SubjectLabelConverter.INSTANCE.convertBOToLabel(subjectLabelBO1);
        List<SubjectLabel> labelList1 = subjectLabelService.queryByCondition(subjectLabel);
        List<SubjectLabelBO> boList1 = SubjectLabelConverter.INSTANCE.convertLabelToBOList(labelList1);
        return boList1;


        /**
         * 涉及到组装，拿到CategotyId去subject_mapping表里查询相应的不重复的labelId,
         * 然后用这些labelId去subject_label表里查询对应的标签信息
         */
//        Long categoryId = subjectLabelBO.getCategoryId();
//        SubjectMapping subjectMapping = new SubjectMapping();
//        subjectMapping.setCategoryId(categoryId);
//        subjectMapping.setIsDeleted(IsDeletedFlagEnum.UN_DELETED.getCode());
//        List<SubjectMapping> mappingList = subjectMappingService.queryLabelId(subjectMapping);//只含有所需要的labelId，其余为空
//        if (CollectionUtils.isEmpty(mappingList)) {
//            return Collections.emptyList();
//        }
//        List<Long> labelIdList = mappingList.stream().map(SubjectMapping::getLabelId).collect((Collectors.toList()));
//        List<SubjectLabel> labelList = subjectLabelService.batchQueryById(labelIdList);
//        List<SubjectLabelBO> boList = new LinkedList<>();
//        labelList.forEach(label -> {
//            SubjectLabelBO bo = new SubjectLabelBO();
//            bo.setId(label.getId());
//            bo.setLabelName(label.getLabelName());
//            bo.setCategoryId(categoryId);
//            bo.setSortNum(label.getSortNum());
//            boList.add(bo);
//        });
//        return boList;
    }
}
